package com.example.jdbcdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.jdbcdemo.mapper.GroupIdMapMapper;
import com.example.jdbcdemo.mapper.OrderMapper;
import com.example.jdbcdemo.mapper.UserMapper;
import com.example.jdbcdemo.pojo.GroupIdMap;
import com.example.jdbcdemo.pojo.Order;
import com.example.jdbcdemo.service.RecordServiceInterface;
import com.example.jdbcdemo.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RecordController {

    @Autowired
    GroupIdMapMapper groupIdMapMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RecordServiceInterface recordServiceInterface;
    @Autowired
    UserMapper userMapper;


    @PostMapping("/record/listdate")
    public JsonUtil queryRecordByDate(@RequestBody Map<String,String> map){
        List dataList = recordServiceInterface.queryGroupByDate(map.get("date"));
        if (dataList==null){
            JsonUtil jsonUtil=new JsonUtil(400,"该日期无记录","[]");
            return jsonUtil;
        }
        JsonUtil jsonUtil=new JsonUtil(0,"查询成功", JSON.toJSONString(dataList));
        return jsonUtil;
    }

    @PostMapping("/record")
    public JsonUtil queryRecordList(){
        List list= recordServiceInterface.queryGroupList();
        if (list==null){
            JsonUtil jsonUtil=new JsonUtil(400,"无变动记录","[]");
            return jsonUtil;
        }
        JsonUtil jsonUtil=new JsonUtil(0,"查询成功",JSON.toJSONString(list));
        return  jsonUtil;
    }

    @PostMapping("/record/listid")
    public JsonUtil queryRecordById(@RequestBody Map<String,Integer> map){
        Map recordMap= recordServiceInterface.queryGroupListById(map.get("id"));
        if (recordMap==null){
            JsonUtil jsonUtil=new JsonUtil(400,"查询失败","");
            return jsonUtil;
        }
        JsonUtil jsonUtil=new JsonUtil(0,"查询成功",JSON.toJSONString(recordMap));
        return jsonUtil;
    }

    @PostMapping("/record/listInputOrder")
    public JsonUtil queryInputRecord(){
        List list=recordServiceInterface.queryInputGroupList();
        if (list==null){
            JsonUtil jsonUtil=new JsonUtil(400,"无进货记录","[]");
            return jsonUtil;
        }
        return new JsonUtil(0,"查询进货记录成功",JSON.toJSONString(list));
    }

    @PostMapping("/record/listSaleOrder")
    public JsonUtil querySaleRecord(){
        List list=recordServiceInterface.querySaleGroupList();
        if (list==null||list.isEmpty()){
            JsonUtil jsonUtil=new JsonUtil(400,"无销售记录","[]");
            return jsonUtil;
        }
        return new JsonUtil(0,"查询销售记录成功",JSON.toJSONString(list));

    }

    //根据groupId退货
    @PostMapping("/record/refundOrder")
    public JsonUtil refundOrder(HttpServletRequest request,  @RequestBody Map<String,Integer> map){
        //操作员id从token获取
        //token中包含了用户名，根据用户名获取用户id
        String token= (String) request.getHeader("token");
        String spiltss[]=token.split("#");
        String userName=spiltss[0];
        int userId=userMapper.queryUserByname(userName).getId();
        //新增权限判断，只有老板和退货员（对应usertype为0和3）能够退货(退款)
        int userType=userMapper.queryUserByname(userName).getUserType();
        if (userType!=0&&userType!=3)
        {
            return new JsonUtil(1000,"权限不足",null);
        }

        return recordServiceInterface.RefundByGroupId(map.get("groupId"));
    }
}
